//
//  BBNodeInfoView.m
//  xNose
//
//  Created by ben smith on 9/10/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BBNodeInfoView.h"
#import "BBNode.h"
#import "BBDefaultController.h"

@implementation BBNodeInfoView

@synthesize showsSubtitle;

-(void)setNode:(BBNode*)aNode
{
	[aNode retain];
	[node release];
	node = aNode;
	[self setupTextFields];
	if (node.title != nil) [titleField setStringValue:node.information];
	if (node.subtitle != nil) [subtitleField setStringValue:node.subtitle];
}

-(NSRect)textRect
{
	return [self bounds];	
}

-(NSFont*)font
{
	return [BBDefaultController infoFont];
}

- (void)setupTextAttributes
{
}

-(void)setupTextFields
{
	NSFont * subFont = [NSFont fontWithName:@"MyriadPro-LightIt" size:14];
	
	NSRect viz = NSInsetRect(self.bounds, 2, 0);
	
	float fieldHeight  = [BBDefaultController fontHeight:subFont];
	NSRect subFrame = NSMakeRect(NSMinX(viz), NSMaxY(viz) - fieldHeight, NSWidth(viz), fieldHeight);
	if (showsSubtitle) {
		subtitleField = [[NSTextField alloc] initWithFrame:subFrame];
		[subtitleField setAlignment:NSCenterTextAlignment];
		[subtitleField setFont:subFont];
		[subtitleField setTextColor:[self fontColor]];
		[subtitleField setDrawsBackground:NO];
		[subtitleField setBordered:NO];
		[subtitleField setTarget:self];
		[subtitleField setAction:@selector(subfieldDone:)];
		[subtitleField setDelegate:self];
		[self addSubview:subtitleField];		
	}
	
		
	NSRect infoFrame = NSMakeRect(NSMinX(viz), NSMinY(viz), NSWidth(viz), NSHeight(viz) - fieldHeight);
	
	if (!showsSubtitle) infoFrame.size.height += fieldHeight;
	
	titleField = [[NSTextField alloc] initWithFrame:infoFrame];
	[titleField setAlignment:NSCenterTextAlignment];
	[titleField setFont:[self font]];
	[titleField setTextColor:[self fontColor]];
	[titleField setDrawsBackground:NO];
	[titleField setBordered:NO];
	[titleField setTarget:self];
	[titleField setAction:@selector(fieldDone:)];
	[titleField setDelegate:self];
	[self addSubview:titleField];
	
	
	if (![[NSUserDefaults standardUserDefaults] boolForKey:@"BBCanEditNodes"]) {
		[titleField setSelectable:NO];
		[titleField setEditable:NO];		
		[subtitleField setSelectable:NO];
		[subtitleField setEditable:NO];		
	}
	
}	

- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
	self.node.information = [titleField stringValue];
	self.node.subtitle = [subtitleField stringValue];
}

- (void)subfieldDone:(id)sender
{
	self.node.subtitle = [subtitleField stringValue];
	[[NSNotificationCenter defaultCenter] postNotificationName:@"BBReleaseEditNotification" object:nil];	
}

- (void)fieldDone:(id)sender
{
	self.node.information = [titleField stringValue];
	[[NSNotificationCenter defaultCenter] postNotificationName:@"BBReleaseEditNotification" object:nil];	
}









@end
